package com.ecube.mqtt;

import com.alibaba.fastjson.JSONObject;
import com.ecube.mqtt.mysql.Query;
import com.ecube.mqtt.service.SwapSendService;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MqttUtils {

    private static final String versionP = "V1.0.8";
    public static Boolean sendStartChargeFailMQTT(String env, String externalCode, String index, String chgID, String gunNo, String result, String reson, String starttime, String ordersn) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"chgDir\": 2,\n" +
                "        \"chgID\": \"" + chgID + "\",\n" +
                "        \"gunNo\": " + gunNo + ",\n" +
                "        \"result\": " + result + ",\n" +
                "        \"reason\": " + reson + ",\n" +
                "        \"orderSn\": \"" + ordersn + "\"\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteChargeStartResp\",\n" +
                "        \"index\": \"" + index + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"version\": \"" + version +  "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendStartChargeMQTT(String env, String externalCode, String index, String chgID, String gunNo, String starttime, String orderSn) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        if (gunNo.equals("12")){
            gunNo="1";
        }
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"chgDir\": 2,\n" +
                "        \"chgID\": \"" + chgID + "\",\n" +
                "        \"gunNo\": " + gunNo + ",\n" +
                "        \"result\": 1,\n" +
                "        \"reason\": null,\n" +
                "        \"orderSn\": \"" + orderSn + "\"\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteChargeStartResp\",\n" +
                "        \"index\": \"" + index + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    public static Boolean sendChargeAuthMQTT(String env, String externalCode, String plateNo, String chgID, String gunNo) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/request";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        String ordersn = Query.getOrderSn(env, chgID);
        String vin = Query.getvin(env, plateNo);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (gunNo.equals("12")){
            gunNo="1";
        }
        String content = "{\n" +
                "  \"dataBody\": {\n" +
                "    \"chgID\": \"" +chgID+ "\",\n" +
                "    \"gunNo\": \"" +gunNo+ "\",\n" +
                "    \"batID\": \"4321742023\",\n" +
                "    \"vin\": \"" + vin + "\",\n" +
                "    \"orderSn\": \"" + ordersn + "\"\n" +
                "  },\n" +
                "  \"header\": {\n" +
                "    \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "    \"reason\": 2,\n" +
                "    \"function\": \"chargeAuthReq\",\n" +
                "    \"index\": 12,\n" +
                "    \"version\": \"" +version+ "\"\n" +
                "  }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    public static Boolean sendRealDataMQTT(String env, String externalCode, String chgID, String gunNo, String soc) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/state";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = Query.getStartTime(env, externalCode);
        String ordersn = Query.getOrderSn(env, chgID);

        String content = "    {\n" +
                "        \"dataBody\": {\n" +
                "            \"chgList\": [\n" +
                "                {\n" +
                "                    \"batMinVoltNo\": 0,\n" +
                "                    \"chgOutputCurr\": 32,\n" +
                "                    \"batMaxVoltNo\": 5,\n" +
                "                    \"batID\": \"120000100100\",\n" +
                "                    \"soc\": " + soc + ",\n" +
                "                    \"batMinTemp\": -4,\n" +
                "                    \"batMinVolt\": 0,\n" +
                "                    \"chgQty\": 3.3,\n" +
                "                    \"batMaxVolt\": 3.42,\n" +
                "                    \"voltDemand\": 700.8,\n" +
                "                    \"cabinetNo\": 1,\n" +
                "                    \"remainderChgTime\": 13,\n" +
                "                    \"chgMode\": 2,\n" +
                "                    \"currDemand\": 50.2,\n" +
                "                    \"batMaxTempNo\": 2,\n" +
                "                    \"startTime\": \"" + startTime + "\",\n" +
                "                    \"batMinTempNo\": 1,\n" +
                "                    \"chgTime\": 32,\n" +
                "                    \"chgOutputVolt\": 26.6,\n" +
                "                    \"chgID\": \"" + chgID + "\",\n" +
                "                    \"batMaxTemp\": -99,\n" +
                "                    \"gunNo\": " + gunNo + ",\n" +
                "                    \"ordersn\": \"" + ordersn + "\",\n" +
                "                    \"dcMeterElect\": 12.1\n" +
                "                }\n" +
                "            ],\n" +
                "            \"chgNum\": 1\n" +
                "        },\n" +
                "        \"header\": {\n" +
                "            \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "            \"reason\": 2,\n" +
                "            \"function\": \"chargingData\",\n" +
                "            \"index\": 77,\n" +
                "            \"version\": \"" + version + "\"\n" +
                "        }\n" +
                "    }";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean send2RealDataMQTT(String env, String externalCode, String chgID, String soc) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/state";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String startTime = Query.getStartTime(env, externalCode);
        String ordersn = Query.getOrderSn(env, chgID);
        String content = "    {\n" +
                "        \"dataBody\": {\n" +
                "            \"chgList\": [\n" +
                "                {\n" +
                "                    \"batMinVoltNo\": 0,\n" +
                "                    \"chgOutputCurr\": 32,\n" +
                "                    \"batMaxVoltNo\": 5,\n" +
                "                    \"batID\": \"120000100100\",\n" +
                "                    \"soc\": " + soc + ",\n" +
                "                    \"batMinTemp\": -4,\n" +
                "                    \"batMinVolt\": 0,\n" +
                "                    \"chgQty\": 5.3,\n" +
                "                    \"batMaxVolt\": 3.42,\n" +
                "                    \"voltDemand\": 700.8,\n" +
                "                    \"cabinetNo\": 1,\n" +
                "                    \"remainderChgTime\": 13,\n" +
                "                    \"chgMode\": 2,\n" +
                "                    \"currDemand\": 50.2,\n" +
                "                    \"batMaxTempNo\": 2,\n" +
                "                    \"startTime\": \"" + startTime + "\",\n" +
                "                    \"batMinTempNo\": 1,\n" +
                "                    \"chgTime\": 32,\n" +
                "                    \"chgOutputVolt\": 26.6,\n" +
                "                    \"chgID\": \"" + chgID + "\",\n" +
                "                    \"batMaxTemp\": -99,\n" +
                "                    \"gunNo\": " + 1 + ",\n" +
                "                    \"ordersn\": \"" + ordersn + "\",\n" +
                "                    \"dcMeterElect\": 12.1\n" +
                "                },\n" +
                "                {\n" +
                "                    \"batMinVoltNo\": 0,\n" +
                "                    \"chgOutputCurr\": 32,\n" +
                "                    \"batMaxVoltNo\": 5,\n" +
                "                    \"batID\": \"120000100100\",\n" +
                "                    \"soc\": " + soc + ",\n" +
                "                    \"batMinTemp\": -4,\n" +
                "                    \"batMinVolt\": 0,\n" +
                "                    \"chgQty\": 1.3,\n" +
                "                    \"batMaxVolt\": 3.42,\n" +
                "                    \"voltDemand\": 700.8,\n" +
                "                    \"cabinetNo\": 1,\n" +
                "                    \"remainderChgTime\": 13,\n" +
                "                    \"chgMode\": 2,\n" +
                "                    \"currDemand\": 50.2,\n" +
                "                    \"batMaxTempNo\": 2,\n" +
                "                    \"startTime\": \"" + startTime + "\",\n" +
                "                    \"batMinTempNo\": 1,\n" +
                "                    \"chgTime\": 32,\n" +
                "                    \"chgOutputVolt\": 26.6,\n" +
                "                    \"chgID\": \"" + chgID + "\",\n" +
                "                    \"batMaxTemp\": -99,\n" +
                "                    \"gunNo\": " + 2 + ",\n" +
                "                    \"ordersn\": \"" + ordersn + "\",\n" +
                "                    \"dcMeterElect\": 12.1\n" +
                "                }\n" +
                "            ],\n" +
                "            \"chgNum\": 2\n" +
                "        },\n" +
                "        \"header\": {\n" +
                "            \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "            \"reason\": 2,\n" +
                "            \"function\": \"chargingData\",\n" +
                "            \"index\": 77,\n" +
                "            \"version\": \"" + version + "\"\n" +
                "        }\n" +
                "    }";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendEndChargeMQTT(String env, String externalCode, String index, String chgID, String gunNo, String endtime) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"chgDir\": 2,\n" +
                "        \"chgID\": \"" + chgID + "\",\n" +
                "        \"gunNo\": " + gunNo + ",\n" +
                "        \"result\": 1,\n" +
                "        \"reason\": null\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteChargeStopResp\",\n" +
                "        \"index\": \"" + index + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"timeStamp\": \"" + endtime + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendEndChargeFailMQTT(String env, String externalCode, String index, String chgID, String gunNo, String endtime,String result) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"chgDir\": 2,\n" +
                "        \"chgID\": \"" + chgID + "\",\n" +
                "        \"gunNo\": " + gunNo + ",\n" +
                "        \"result\": " + result + ",\n" +
                "        \"reason\": null\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteChargeStopResp\",\n" +
                "        \"index\": \"" + index + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"timeStamp\": \"" + endtime + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendChargeResultMQTT(String env, String externalCode, String chgID, String index, String gunNo, String plateNo, String time) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/event";
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() + Integer.parseInt(time)*60*1000;
        String endtime = dateFormat.format(t);
        String starttime = dateFormat.format(date);
        String rateModelID = Query.getChargeModelId(env, externalCode);
        String modelID = Query.getModelId(env, externalCode);
        String version = Query.getVersion(env,externalCode);
        if (gunNo.equals("12")){
            gunNo="1";
        }
        //startTime加上充电时长等于结束时间
        List<Map<String, String>> times = Query.getbillingTimeList(env, modelID, starttime, endtime);
        int sharp = 0;
        int peak = 0;
        int flat = 0;
        int valley = 0;
        int deepValley = 0;
        int count = 0;
        String detailsList ="";
        for (int i = 0; i < times.size(); i++){
            if(times.get(i).get("ratetype").equals("1")){
                sharp ++;
            }
            if(times.get(i).get("ratetype").equals("2")){
                peak ++;
            }
            if(times.get(i).get("ratetype").equals("3")){
                flat ++;
            }
            if(times.get(i).get("ratetype").equals("4")){
                valley ++;
            }
            if(times.get(i).get("ratetype").equals("5")){
                deepValley ++;
            }
            String details = "{\n" +
                    "                \"rateType\": " + times.get(i).get("ratetype") + ",\n" +
                    "                \"startTime\": \"" + times.get(i).get("start_time") + "\",\n" +
                    "                \"stopTime\": \"" + times.get(i).get("end_time") + "\",\n" +
                    "                \"elect\": 35\n" +
                    "            }";
            if (i ==0){
                detailsList = details;
            }else {
                detailsList = detailsList + "," +details;
            }
        }
        count = sharp + peak + flat + valley + deepValley;
        double stopElect = count*35 +25.5;
        //String detailsList = JSONObject.toJSONString(times);
        String vin = Query.getvin(env, plateNo);
        String ordersn = Query.getOrderSn(env, chgID);
        if(ordersn==null){
            ordersn = SwapSendService.getRandom2(18);
        }
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"gunNo\": " + gunNo + ",\n" +
                "        \"startElect\": 25.5,\n" +
                "        \"orderSn\": \"" + ordersn + "\",\n" +
                "        \"chgSwitch\": 1,\n" +
                "        \"stopSoc\": 100,\n" +
                "        \"detailsList\": [" + detailsList + "],\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"startTime\": \"" + starttime + "\",\n" +
                "        \"stopTime\": \"" + endtime + "\",\n" +
                "        \"stopElect\": " + stopElect + ",\n" +
                "        \"rateModelID\": \"" + rateModelID + "\",\n" +
                "        \"equipNo\": \""+ chgID +"\",\n" +
                "        \"chgTime\": 1200,\n" +
                "        \"stopReason\": 52,\n" +
                "        \"batID\": \"120000100100\",\n" +
                "        \"plateNo\": \"" + plateNo + "\",\n" +
                "        \"startType\": 4,\n" +
                "        \"snno\": 42,\n" +
                "        \"sharpElect\": " + sharp*35 + ",\n" +
                "        \"peakElect\": " + peak*35 + ",\n" +
                "        \"flatElect\": " + flat*35 + ",\n" +
                "        \"valleyElect\": " + valley*35 + ",\n" +
                "        \"deepValleyElect\": " + deepValley*35 + ",\n" +
                "        \"swapSn\": \"\",\n" +
                "        \"startSoc\": 10.2,\n" +
                "        \"electAmount\": " + count*35 + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + endtime + "\",\n" +
                "        \"function\": \"chargeRecord\",\n" +
                "        \"index\": 42,\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendCEFailMQTT(String env, String externalCode, String ordersn, String plateNo, String result, String reson) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String rfidCode = Query.getrfID(env, plateNo);
        String vin = Query.getvin(env, plateNo);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"result\": " + result + ",\n" +
                "        \"orderSn\": \"" + ordersn + "\",\n" +
                "        \"plateNo\": \"" + plateNo + "\",\n" +
                "        \"rfidCode\": \"" + rfidCode + "\",\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"reason\": " + reson + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteSwapStartResp\",\n" +
                "        \"index\": 54,\n" +
                "        \"reason\": 0,\n" +
                "        \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\":\"" + dateFormat.format(date) + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendCEMQTT(String env, String externalCode, String ordersn, String plateNo) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String rfidCode = Query.getrfID(env, plateNo);
        String vin = Query.getvin(env, plateNo);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"result\": 1,\n" +
                "        \"orderSn\": \"" + ordersn + "\",\n" +
                "        \"plateNo\": \"" + plateNo + "\",\n" +
                "        \"rfidCode\": \"" + rfidCode + "\",\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"reason\": 0\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"remoteSwapStartResp\",\n" +
                "        \"index\": 54,\n" +
                "        \"reason\": 0,\n" +
                "        \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\":\"" + dateFormat.format(date) + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendCEResponse(String env, String externalCode, String ordersn, String plateNo, String model, String starttime, String startSoc,
                                         String endSoc) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/event";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        Date stopTime = new Date(date.getTime() + 5*60*1000);

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rfidCode = Query.getrfID(env, plateNo);
        String vin = Query.getvin(env, plateNo);
        String swapMode ="";
        String swapStartType = "";
        if (model.equals("云端")) {
            swapMode = "1";
            swapStartType = "17";
        }else if (model.equals("站控")){
            swapMode = "1";
            swapStartType = "1";
        }else if (model.equals("手动")){
            swapMode = "3";
            swapStartType = "1";
        }

        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "    \"equipNo\": \"" + ordersn + "\",\n" +
                "    \"orderSn\": \"" + ordersn + "\",\n" +
                "            \"startTime\":\"" + starttime + "\",\n" +
                "            \"stopTime\":\"" + dateFormat.format(stopTime) + "\",\n" +
                "            \"vin\":\"" + vin + "\",\n" +
                "            \"rfidCode\":\"" + rfidCode + "\",\n" +
                "            \"plateNo\":\"" + plateNo + "\",\n" +
                "            \"newBatID\":\"120000110011\",\n" +
                "            \"newCabinetNo\":1,\n" +
                "            \"newBatSoc\":" + endSoc + ",\n" +
                "            \"oldBatID\":\"120000110000\",\n" +
                "            \"oldCabinetNo\":2,\n" +
                "            \"oldBatSoc\":"+ startSoc +",\n" +
                "            \"swapType\":2,\n" +
                "            \"swapMode\":" + swapMode + ",\n" +
                "            \"lane\":1,\n" +
                "            \"userId\": \"-\",\n" +
                "            \"swapStartType\":" + swapStartType + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "    \"function\": \"swapRecord\",\n" +
                "    \"index\": 3177,\n" +
                "    \"reason\": \"4\",\n" +
                "    \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\":\"" + dateFormat.format(date) + "\"\n" +
                "    }\n" +
                "    }";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendCEResponse1(String env, String externalCode, String ordersn, String plateNo, String model, String starttime, String soc) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/event";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String rfidCode = Query.getrfID(env, plateNo);
        String vin = Query.getvin(env, plateNo);
        String swapMode ="";
        String swapStartType = "";
        if (model.equals("云端")) {
            swapMode = "1";
            swapStartType = "17";
        }else if (model.equals("站控")){
            swapMode = "1";
            swapStartType = "1";
        }else if (model.equals("手动")){
            swapMode = "3";
            swapStartType = "1";
        }

        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "    \"equipNo\": \"" + ordersn + "\",\n" +
                "    \"orderSn\": \"" + ordersn + "\",\n" +
                "            \"startTime\":\"" + starttime + "\",\n" +
                "            \"stopTime\":\"" + dateFormat.format(date) + "\",\n" +
                "            \"vin\":\"" + vin + "\",\n" +
                "            \"rfidCode\":\"" + rfidCode + "\",\n" +
                "            \"plateNo\":\"" + plateNo + "\",\n" +
                "            \"newBatID\":\"120000110011\",\n" +
                "            \"newCabinetNo\":1,\n" +
                "            \"newBatSoc\":10,\n" +
                "            \"oldBatID\":\"120000110000\",\n" +
                "            \"oldCabinetNo\":2,\n" +
                "            \"oldBatSoc\":" + soc + ",\n" +
                "            \"swapType\":2,\n" +
                "            \"swapMode\":" + swapMode + ",\n" +
                "            \"lane\":1,\n" +
                "            \"userId\": \"-\",\n" +
                "            \"swapStartType\":" + swapStartType + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "    \"function\": \"swapRecord\",\n" +
                "    \"index\": 3177,\n" +
                "    \"reason\": \"4\",\n" +
                "    \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\":\"" + dateFormat.format(date) + "\"\n" +
                "    }\n" +
                "    }";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendModelMQTT(String env, String externalCode) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/response";
        String version = Query.getVersion(env,externalCode);
        String modelID = Query.getChargeModelId(env, externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"rateModelID\": \"" + modelID + "\",\n" +
                "        \"result\": 1,\n" +
                "        \"reason\": null\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"rateModeSyncResp\",\n" +
                "        \"index\": \"12640\",\n" +
                "        \"reason\": 3,\n" +
                "        \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    //充电枪业务
    //充电枪状态
    public static Boolean sendChgStateMQTT1(String env,String stationId, String chgID, String gunno, String state, String batConn, String starttime) {
        String pubTopic = Query.getProductCode(env, stationId) + stationId + "/S2M/state";
        String version = Query.getVersion(env, stationId);
        String sversion = Query.getSVersion(env, stationId);
        String plug = "plugWay";
        if (sversion.equals("v3.0")) {
            plug="plugSameBat";
        }
        String content = "{\n" +
                "    \"dataBody\": [\n" +
                "        {\n" +
                "            \"batState\": 1,\n" +
                "            \"cabinetNo\": 1,\n" +
                "            \"chgFault\": \"\",\n" +
                "            \"chgID\": \"" + chgID + "\",\n" +
                "            \"chgSwitch\": 1,\n" +
                "            \"door\": 1,\n" +
                "            \"emergStop\": 1,\n" +
                "            \"" + plug + "\": 1,\n" +
                "            \"gunList\": [\n" +
                "                {\n" +
                "                    \"batConn\": " + batConn + ",\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"batID\": 120000100100,\n" +
                "                    \"chgState\": " + state + ",\n" +
                "                    \"gunNo\": " + gunno + ",\n" +
                "                    \"soc\": 50,\n" +
                "                    \"workMode\": 1\n" +
                "                }\n" +
                "            ],\n" +
                "            \"gunNum\": 1\n" +
                "        }\n" +
                "    ],\n" +
                "    \"header\": {\n" +
                "        \"function\": \"chargerState\",\n" +
                "        \"index\": 11,\n" +
                "        \"reason\": 2,\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    //充电枪业务
    //充电枪状态
    public static Boolean send2ChgStateMQTT1(String env, String stationId, String chgID, String state, String batConn, String starttime) {
        String pubTopic = Query.getProductCode(env, stationId) + stationId + "/S2M/state";
        String version = Query.getVersion(env, stationId);
        String sversion = Query.getSVersion(env, stationId);
        String plug = "plugWay";
        if (sversion.equals("v3.0")) {
            plug="plugSameBat";
        }
        String content = "{\n" +
                "    \"dataBody\": [\n" +
                "        {\n" +
                "            \"batState\": 1,\n" +
                "            \"cabinetNo\": 1,\n" +
                "            \"chgFault\": \"\",\n" +
                "            \"chgID\": \"" + chgID + "\",\n" +
                "            \"chgSwitch\": 1,\n" +
                "            \"door\": 1,\n" +
                "            \"emergStop\": 1,\n" +
                "            \""+ plug +"\": 2,\n" +
                "            \"gunList\": [\n" +
                "                {\n" +
                "                    \"batConn\": " + batConn + ",\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"batID\": 120000100100,\n" +
                "                    \"chgState\": " + state + ",\n" +
                "                    \"gunNo\": " + 1 + ",\n" +
                "                    \"soc\": 50,\n" +
                "                    \"workMode\": 1\n" +
                "                },\n" +
                "       {\n" +
        "                    \"batConn\": " + batConn + ",\n" +
                "                    \"batFault\": \"\",\n" +
                "                    \"batID\": 120000100100,\n" +
                "                    \"chgState\": " + state + ",\n" +
                "                    \"gunNo\": " + 2 + ",\n" +
                "                    \"soc\": 50,\n" +
                "                    \"workMode\": 1\n" +
                "                }\n" +
                "            ],\n" +
                "            \"gunNum\": 2\n" +
                "        }\n" +
                "    ],\n" +
                "    \"header\": {\n" +
                "        \"function\": \"chargerState\",\n" +
                "        \"index\": 11,\n" +
                "        \"reason\": 2,\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"version\": \"" + version + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendCEChargeEventResponse(String env, String externalCode, String swapsn, String ordersn, String plateNo, String starttime, String time, String startSoc, String endSoc) {
        String pubTopic = Query.getProductCode(env, externalCode) + externalCode + "/S2M/event";
        String version = Query.getVersion(env,externalCode);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() + Integer.parseInt(time)*60*1000;
        String endtime = dateFormat.format(t);

        String vin = Query.getvin(env, plateNo);
        String rateModelID = Query.getChargeModelId(env, externalCode);
        String modelID = Query.getModelId(env, externalCode);
        List<Map<String, String>> times = Query.getbillingTimeList(env, modelID, starttime, endtime);
        int sharp = 0;
        int peak = 0;
        int flat = 0;
        int valley = 0;
        int deepValley = 0;
        int count = 0;
        String detailsList ="";
        for (int i = 0; i < times.size(); i++){
            if(times.get(i).get("ratetype").equals("1")){
                sharp ++;
            }
            if(times.get(i).get("ratetype").equals("2")){
                peak ++;
            }
            if(times.get(i).get("ratetype").equals("3")){
                flat ++;
            }
            if(times.get(i).get("ratetype").equals("4")){
                valley ++;
            }
            if(times.get(i).get("ratetype").equals("5")){
                deepValley ++;
            }
            String details = "{\n" +
                    "                \"rateType\": " + times.get(i).get("ratetype") + ",\n" +
                    "                \"startTime\": \"" + times.get(i).get("start_time") + "\",\n" +
                    "                \"stopTime\": \"" + times.get(i).get("end_time") + "\",\n" +
                    "                \"elect\": 15\n" +
                    "            }";
            if (i ==0){
                detailsList = details;
            }else {
                detailsList = detailsList + "," +details;
            }
        }
        count = sharp + peak + flat + valley + deepValley;
        double stopElect = count*15 + 2287.23;

        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"detailsList\": [\n" +
                detailsList +
                "        ],\n" +
                "        \"chgSwitch\": 0,\n" +
                "        \"chgTime\": " + time + ",\n" +
                "        \"electAmount\": " + count*15 + ",\n" +
                "        \"equipNo\": \"234273929083\",\n" +
                "        \"gunNo\": 0,\n" +
                "        \"orderSn\": \"" + ordersn + "\",\n" +
                "        \"swapSn\": \"" + swapsn + "\",\n" +
                "        \"batID\": \"120000110000\",\n" +
                "        \"sharpElect\": " + sharp*15 + ",\n" +
                "        \"peakElect\": " + peak*15 + ",\n" +
                "        \"flatElect\": " + flat*15 + ",\n" +
                "        \"valleyElect\": " + valley*15 + ",\n" +
                "        \"deepValleyElect\": " + deepValley*15 + ",\n" +
                "        \"plateNo\": \"" + plateNo + "\",\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"rateModelID\": \"" + rateModelID + "\",\n" +
                "        \"startElect\": 2287.23,\n" +
                "        \"startSoc\": " + startSoc + ",\n" +
                "        \"startTime\": \"" + starttime + "\",\n" +
                "        \"stopElect\": " + stopElect + ",\n" +
                "        \"stopReason\": 53,\n" +
                "        \"stopSoc\": " + endSoc + ",\n" +
                "        \"stopTime\": \"" + endtime + "\"\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"function\": \"chargeRecord\",\n" +
                "        \"index\": 55,\n" +
                "        \"reason\": 0,\n" +
                "        \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\":\"" + dateFormat.format(date) + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendMQTT(String env, String pubTopic, String content) {
        try {
            int qos = 2;
            String broker = "";
            if (env.equals("UAT")) {
                broker = "tcp://10.4.6.74:12883";
            } else if (env.equals("FAT")) {
               broker = "tcp://10.5.6.74:1883";
                // broker = "tcp://10.5.215.40:1883";
            } else if (env.equals("DEV")) {
                broker = "tcp://10.6.6.73:1883";
            } else if (env.equals("HG")) {
                broker = "tcp://10.9.25.100:1883";
            }
            String clientId = "mqttx_test";
            MemoryPersistence persistence = new MemoryPersistence();
            MqttClient client = new MqttClient(broker, clientId, persistence);

            // MQTT 连接选项
            MqttConnectOptions connOpts = new MqttConnectOptions();
            connOpts.setUserName("admin");
            connOpts.setPassword("veZgHjmY".toCharArray());

            if (env.equals("HG")) {
                connOpts.setUserName("admin");
                connOpts.setPassword("rhdk@2024".toCharArray());
            }else{
                connOpts.setUserName("admin");
                connOpts.setPassword("veZgHjmY".toCharArray());
            }
            // 保留会话
            connOpts.setCleanSession(true);

            // 设置回调
            client.setCallback(new PushCallback());

            // 建立连接
            System.out.println("Connecting to broker: " + broker);
            client.connect(connOpts);

            System.out.println("Connected");
            System.out.println("Topic: " + pubTopic);
            System.out.println("Publishing message: " + content);

            // 订阅
            //client.subscribe(subTopic);

            // 消息发布所需参数
            MqttMessage message = new MqttMessage(content.getBytes());
            message.setQos(qos);
            client.publish(pubTopic, message);
            System.out.println("Message published");

            client.disconnect();
            System.out.println("Disconnected");
            client.close();
        } catch (MqttException me) {
            System.out.println("reason " + me.getReasonCode());
            System.out.println("msg " + me.getMessage());
            System.out.println("loc " + me.getLocalizedMessage());
            System.out.println("cause " + me.getCause());
            System.out.println("excep " + me);
            me.printStackTrace();
            return false;
        }
        return true;
    }

    //充电桩业务
    //启动充电
    public static Boolean sendStartPileChargeMQTT(String env, String orderSn, String index, String chgID, String gunNo, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/response";
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"result\": 1,\n" +
                "        \"reason\": null,\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"gunCode\": " + gunNo + ",\n" +
                "        \"orderSn\": \"" + orderSn + "\"\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"function\": \"chargeStartRes\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    //充电桩业务
    //启动状态模拟
    public static Boolean sendStartFailPileChargeMQTT(String env, String orderSn, String index, String chgID, String gunNo, String result, String reson, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/response";
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"result\": " + result + ",\n" +
                "        \"reason\": " + reson + ",\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"gunCode\": " + gunNo + ",\n" +
                "        \"orderSn\": \"" + orderSn + "\"\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"function\": \"chargeStartRes\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    //充电桩业务
    //充电桩枪状态
    public static Boolean sendChgStateMQTT(String env, String chgID, String spotstate, String stateA, String stateB, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/state";
        Date date = new Date();
        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
        String rateModelID = "111";
        try {
            rateModelID = Query.getSwapChargeModelId(env, chgID);
        }catch (Exception e) {
            System.out.println("未查到rateModelID");
        }

        String content = "{\n" +
                "  \"dataBody\": {\n" +
                "    \"code\": \"" + chgID + "\",\n" +
                "    \"state\": " + spotstate + ",\n" +
                "    \"softwareVersion\": \"V4.0.1\",\n" +
                "    \"hardwareVersion\": \"01.02\",\n" +
                "    \"startDate\": \"" + dateFormat.format(date) + "\",\n" +
                "    \"chgNum\": 2,\n" +
                "    \"rateModelID\": \"" + rateModelID + "\",\n" +
                "    \"lng\": 120.39747,\n" +
                "    \"lat\": 30.90869,\n" +
                "    \"networkConn\": 88,\n" +
                "    \"gunList\": [\n" +
                "      {\n" +
                "        \"code\": 1,\n" +
                "        \"state\": " + stateA+ ",\n" +
                "        \"chgMode\": 1,\n" +
                "        \"elockConn\": 1,\n" +
                "        \"vehicleConn\": 1,\n" +
                "        \"bmsConn\": 1,\n" +
                "        \"apsConn\": 1\n" +
                "      },\n" +
                "      {\n" +
                "        \"code\": 2,\n" +
                "        \"state\": " + stateB + ",\n" +
                "        \"chgMode\": 1,\n" +
                "        \"elockConn\": 1,\n" +
                "        \"vehicleConn\": 1,\n" +
                "        \"bmsConn\": 1,\n" +
                "        \"apsConn\": 1\n" +
                "      }\n" +
                "    ]\n" +
                "  },\n" +
                "  \"header\": {\n" +
                "    \"function\": \"chargerState\",\n" +
                "    \"index\": 5505,\n" +
                "    \"reason\": 2,\n" +
                "    \"timeStamp\": \"" + starttime + "\",\n" +
                "    \"version\": \"" + versionP + "\"\n" +
                "  }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    public static Boolean sendAuthenticationMQTT(String env, String orderSn, String index, String chgID, String gunNo, String vin, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/request";
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"batID\": \"\",\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"gunCode\": " + gunNo + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeAuthReq\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendBMS(String env, String orderSn, String index, String chgID, String vin, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/event";
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"time\": \"" + starttime + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"code\": 3,\n" +
                "        \"desc\": \"stop\",\n" +
                "        \"chgSn\": \"" + orderSn + "\",\n" +
                "        \"value\": {\n" +
                "            \"bmsProtocolVersion\": \"v1.0.1\",\n" +
                "            \"totalVolt\": 15,\n" +
                "            \"batMaxChgVolt\": 15,\n" +
                "            \"batMaxChgCurr\": 20,\n" +
                "            \"totalCapacity\": 282,\n" +
                "            \"maxChgVolt\": 20,\n" +
                "            \"maxTemp\": 20,\n" +
                "            \"volt\": 20,\n" +
                "            \"soc\": 20.0,\n" +
                "            \"batID\": 12700454414,\n" +
                "            \"vin\": \"" + vin + "\"\n" +
                "        }\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeEvent\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendPileStartMQTT(String env, String orderSn, String index, String chgID, String starttime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/event";
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"time\": \"" + starttime + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"code\": 5,\n" +
                "        \"desc\": \"start\",\n" +
                "        \"chgSn\": \"" + orderSn + "\",\n" +
                "        \"value\": {\n" +
                "            \"batID\": \"888866668888\",\n" +
                "            \"startSoc\": 50\n" +
                "        }\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + starttime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeEvent\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendPileStateMQTT(String env, String soc, String index, String chgID, String vin, String gunNo) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/state";
        String startTime = Query.getPileStartTime(env, chgID);

        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"totalChgTime\": 5,\n" +
                "        \"MinTemp\": 20,\n" +
                "        \"soc\": " + soc + ",\n" +
                "        \"capacity\": 3446,\n" +
                "        \"voltDemand\": 450,\n" +
                "        \"remainderChgTime\": 89,\n" +
                "        \"chgVolt\": 0,\n" +
                "        \"currDemand\": 100,\n" +
                "        \"MinVoltNo\": -999,\n" +
                "        \"startTime\": \"" + startTime + "\",\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"chgTime\": 20,\n" +
                "        \"chgOutputVolt\": 0,\n" +
                "        \"batType\": 3,\n" +
                "        \"batID\": \"1\",\n" +
                "        \"chgOutputCurr\": 0,\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"MaxVoltNo\": 0,\n" +
                "        \"totalChgCount\": 16777215,\n" +
                "        \"gunCodeArray\": [\n" +
                "            " + gunNo + "\n" +
                "        ],\n" +
                "        \"snno\": " + index + ",\n" +
                "        \"MaxVolt\": 3,\n" +
                "        \"chgQty\": 20,\n" +
                "        \"MinTempNo\": 0,\n" +
                "        \"MaxTemp\": 31,\n" +
                "        \"MinVolt\": -999,\n" +
                "        \"productCode\": \"100040002\",\n" +
                "        \"dcMeterElect\": 17,\n" +
                "        \"chgMode\": 2,\n" +
                "        \"MaxTempNo\": 0,\n" +
                "        \"chgCurr\": 0\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "        \"reason\": 2,\n" +
                "        \"function\": \"chargeState\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }


    public static Boolean sendStopPileChargeMQTT(String env, String orderSn, String index, String chgID, String gunNo, String stoptime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/response";
        String content = "   {\n" +
                "    \"dataBody\": {\n" +
                "        \"timeStamp\": \"" + stoptime + "\",\n" +
                "        \"result\": 1,\n" +
                "        \"reason\": 0,\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"gunCode\": " + gunNo + "\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + stoptime + "\",\n" +
                "        \"reason\": 3,\n" +
                "        \"function\": \"chargeStopRes\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendStopPileEventChargeMQTT(String env, String orderSn, String index, String chgID, String stoptime) {
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/event";
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"time\": \"" + stoptime + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"code\": 2,\n" +
                "        \"desc\": \"stop\",\n" +
                "        \"chgSn\": \"" + orderSn + "\",\n" +
                "        \"value\": {\n" +
                "            \"minVolt\": 1,\n" +
                "            \"maxVolt\": 1,\n" +
                "            \"minTemp\": 1,\n" +
                "            \"maxTemp\": 1\n" +
                "        }\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + stoptime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeEvent\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static String getEventJson(String env, String orderSn, String index, String chgID, String vin, String gunNo, String rateModelID) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() - 120000;
        String endtime = dateFormat.format(date);
        String starttime = dateFormat.format(t);

        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/event";
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"gunCodeArr\": [\n" +
                "            " + gunNo + "\n" +
                "        ],\n" +
                "        \"startTime\": \"" + starttime + "\",\n" +
                "        \"stopTime\": \"" + endtime + "\",\n" +
                "        \"startSoc\": 26.0,\n" +
                "        \"stopSoc\": 100.0,\n" +
                "        \"electAmount\": 100.0,\n" +
                "        \"totalMoney\": 6.0,\n" +
                "        \"electMoney\": 4,\n" +
                "        \"serviceMoney\": 2,\n" +
                "        \"startElect\": 26.0,\n" +
                "        \"stopElect\": 126.0,\n" +
                "        \"chgTime\": 400,\n" +
                "        \"batId\": 12700454414,\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"sharpElect\": 100.0,\n" +
                "        \"sharpMoney\": 100.0,\n" +
                "        \"sharpElectMoney\": 4,\n" +
                "        \"sharpServiceMoney\": 2,\n" +
                "        \"peakElect\": 0,\n" +
                "        \"peakMoney\": 0,\n" +
                "        \"peakElectMoney\": 0,\n" +
                "        \"peakServiceMoney\": 0,\n" +
                "        \"flatElect\": 0,\n" +
                "        \"flatMoney\": 0,\n" +
                "        \"flatElectMoney\": 0,\n" +
                "        \"flatServiceMoney\": 0,\n" +
                "        \"valleyElect\": 0,\n" +
                "        \"valleyMoney\": 0,\n" +
                "        \"valleyElectMoney\": 0,\n" +
                "        \"valleyServiceMoney\": 0,\n" +
                "        \"rateModelID\": \"" + rateModelID + "\",\n" +
                "        \"detailsList\": [\n" +
                "            {\n" +
                "                \"rateType\": 1,\n" +
                "                \"startTime\": \"" + starttime + "\",\n" +
                "                \"stopTime\": \"" + endtime + "\",\n" +
                "                \"elect\": 100,\n" +
                "                \"electMoney\": 4,\n" +
                "                \"serviceMoney\": 2\n" +
                "            }\n" +
                "        ]\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + endtime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeRecord\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";
        System.out.println(pubTopic);
        System.out.println(content);
        return pubTopic + content;
    }

    public static Boolean sendPileEventMQTT(String env, String orderSn, String index, String chgID, String vin, String gunNo, String rateModelID, String time) {
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime() + Integer.parseInt(time)*60*1000;
        String endtime = dateFormat.format(t);
        String starttime = dateFormat.format(date);
        String modelID = Query.getPileModelId(env, rateModelID);
        List<Map<String, String>> times = Query.getbillingTimeList(env, modelID, starttime, endtime);
        int sharp = 0;
        int peak = 0;
        int flat = 0;
        int valley = 0;
        int deepValley = 0;
        int count = 0;
        Float totalMoney = 0.00F;
        Float electCountMoney = 0.00F;
        Float serviceCountMoney = 0.00F;
        int feetype = Query.getPileFeeType(env, rateModelID);
        String detailsList ="";

        Float sharpserviceprice = 0.000000F;
        Float peakserviceprice = 0.000000F;
        Float flatserviceprice = 0.000000F;
        Float valleyserviceprice = 0.000000F;
        Float deepValleyserviceprice = 0.000000F;
        int servicefeetype = Query.getPileServiceFeeType(env, rateModelID);
        if(servicefeetype == 1){
            sharpserviceprice = Query.getPileServicePrice(env, rateModelID, "unit_service_charge");
            peakserviceprice = sharpserviceprice;
            flatserviceprice = sharpserviceprice;
            valleyserviceprice = sharpserviceprice;
            deepValleyserviceprice = sharpserviceprice;
        }else if(servicefeetype == 2){
            Float serviceprice = Query.getPileServicePrice(env, rateModelID, "unit_service_charge");
            Float electprice1 = Query.getPilePrice(env, rateModelID, "1");
            sharpserviceprice = (serviceprice - electprice1);
            Float electprice2 = Query.getPilePrice(env, rateModelID, "2");
            peakserviceprice = (serviceprice - electprice2);
            Float electprice3 = Query.getPilePrice(env, rateModelID, "3");
            flatserviceprice = (serviceprice - electprice3);
            Float electprice4 = Query.getPilePrice(env, rateModelID, "4");
            valleyserviceprice = (serviceprice - electprice4);
            Float electprice5 = Query.getPilePrice(env, rateModelID, "5");
            deepValleyserviceprice = (serviceprice - electprice5);
        }else {
            sharpserviceprice = Query.getPileServicePrice(env, rateModelID, "1");
            peakserviceprice = Query.getPileServicePrice(env, rateModelID, "2");
            flatserviceprice = Query.getPileServicePrice(env, rateModelID, "3");
            valleyserviceprice = Query.getPileServicePrice(env, rateModelID, "4");
            deepValleyserviceprice = Query.getPileServicePrice(env, rateModelID, "5");
        }

        for (int i = 0; i < times.size(); i++){
            if(times.get(i).get("ratetype").equals("1")){
                sharp ++;
            }
            if(times.get(i).get("ratetype").equals("2")){
                peak ++;
            }
            if(times.get(i).get("ratetype").equals("3")){
                flat ++;
            }
            if(times.get(i).get("ratetype").equals("4")){
                valley ++;
            }
            if(times.get(i).get("ratetype").equals("5")){
                deepValley ++;
            }
            Float electMoney = 0.00f;
            if(feetype == 1){
                electMoney = Query.getPilePrice(env, rateModelID, "unit_price")*35;
            }else {
                electMoney = Query.getPilePrice(env, rateModelID, times.get(i).get("ratetype"))*35;
            }

            Float serviceMoney = 0.00F;
            if(servicefeetype == 1){
                serviceMoney = Query.getPileServicePrice(env, rateModelID, "unit_service_charge")*35;
            }else if(servicefeetype == 2){
                Float serviceprice = Query.getPileServicePrice(env, rateModelID, "unit_service_charge");
                Float electprice = Query.getPilePrice(env, rateModelID, times.get(i).get("ratetype"));
                serviceMoney = (serviceprice - electprice)*35;
            }else {
                serviceMoney = Query.getPileServicePrice(env, rateModelID, times.get(i).get("ratetype"))*35;
            }
            electCountMoney+=electMoney;
            totalMoney+=electMoney;
            serviceCountMoney+=serviceMoney;
            totalMoney+=serviceMoney;
            String details = "{\n" +
                    "                \"rateType\": " + times.get(i).get("ratetype") + ",\n" +
                    "                \"startTime\": \"" + times.get(i).get("start_time") + "\",\n" +
                    "                \"stopTime\": \"" + times.get(i).get("end_time") + "\",\n" +
                    "                \"elect\": 35,\n" +
                    "                \"electMoney\": " + String.format("%.2f", electMoney) + ",\n" +
                    "                \"serviceMoney\": " + String.format("%.2f", serviceMoney) + "\n" +
                    "            }";
            if (i ==0){
                detailsList = details;
            }else {
                detailsList = detailsList + "," +details;
            }
        }
        count = sharp + peak + flat + valley + deepValley;
        double stopElect = count*35 +26;
        Random random = new Random();
        int randomNumber = random.nextInt(98);
        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/event";
        String content = "{\n" +
                "    \"dataBody\": {\n" +
                "        \"chgCode\": \"" + chgID + "\",\n" +
                "        \"orderSn\": \"" + orderSn + "\",\n" +
                "        \"gunCodeArr\": [\n" +
                "            " + gunNo + "\n" +
                "        ],\n" +
                "        \"startTime\": \"" + starttime + "\",\n" +
                "        \"stopTime\": \"" + endtime + "\",\n" +
                "        \"startSoc\": " + randomNumber + ",\n" +
                "        \"stopSoc\": 100.0,\n" +
                "        \"electAmount\": " + count*35 + ",\n" +
                "        \"totalMoney\": " + String.format("%.2f", totalMoney) + ",\n" +
                "        \"electMoney\": " + electCountMoney + ",\n" +
                "        \"serviceMoney\": " + serviceCountMoney +",\n" +
                "        \"startElect\": 26.0,\n" +
                "        \"stopElect\": " + stopElect + ",\n" +
                "        \"chgTime\": " + time + ",\n" +
                "        \"batId\": 12700454414,\n" +
                "        \"vin\": \"" + vin + "\",\n" +
                "        \"sharpElect\": " + sharp*35 + ",\n" +
                "        \"sharpMoney\": " + sharp*35*(Query.getPilePrice(env, rateModelID, "1")+sharpserviceprice) + ",\n" +
                "        \"sharpElectMoney\": " + sharp*35*Query.getPilePrice(env, rateModelID, "1") + ",\n" +
                "        \"sharpServiceMoney\": " + sharp*35*sharpserviceprice + ",\n" +
                "        \"peakElect\": " + peak*35 + ",\n" +
                "        \"peakMoney\": " + peak*35*(Query.getPilePrice(env, rateModelID, "2")+peakserviceprice) + ",\n" +
                "        \"peakElectMoney\": " + peak*35*Query.getPilePrice(env, rateModelID, "2") + ",\n" +
                "        \"peakServiceMoney\": " + peak*35*peakserviceprice + ",\n" +
                "        \"flatElect\": " + flat*35 + ",\n" +
                "        \"flatMoney\": " + flat*35*(Query.getPilePrice(env, rateModelID, "3")+flatserviceprice) + ",\n" +
                "        \"flatElectMoney\": " + flat*35*Query.getPilePrice(env, rateModelID, "3") + ",\n" +
                "        \"flatServiceMoney\": " + flat*35*flatserviceprice + ",\n" +
                "        \"valleyElect\": " + valley*35 + ",\n" +
                "        \"valleyMoney\": " + valley*35*(Query.getPilePrice(env, rateModelID, "4")+valleyserviceprice) + ",\n" +
                "        \"valleyElectMoney\": " + valley*35*Query.getPilePrice(env, rateModelID, "4") + ",\n" +
                "        \"valleyServiceMoney\": " + valley*35*valleyserviceprice + ",\n" +
                "        \"deepValleyElect\": " + deepValley*35 + ",\n" +
                "        \"deepValleyMoney\": " + deepValley*35*(Query.getPilePrice(env, rateModelID, "5")+deepValleyserviceprice) + ",\n" +
                "        \"deepValleyElectMoney\": " + deepValley*35*Query.getPilePrice(env, rateModelID, "5") + ",\n" +
                "        \"deepValleyServiceMoney\": " + deepValley*35*deepValleyserviceprice + ",\n" +
                "        \"rateModelID\": \"" + rateModelID + "\",\n" +
                "        \"detailsList\": [\n" + detailsList +
                "        ]\n" +
                "    },\n" +
                "    \"header\": {\n" +
                "        \"timeStamp\": \"" + endtime + "\",\n" +
                "        \"reason\": 1,\n" +
                "        \"function\": \"chargeRecord\",\n" +
                "        \"index\": " + index + ",\n" +
                "        \"version\": \"" + versionP + "\"\n" +
                "    }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static Boolean sendRateMQTT(String env, String chgID, String time) {

        String pubTopic = Query.getPileCode(env, chgID) + chgID + "/S2M/response";
        String rateModelID = Query.getSwapChargeModelId(env, chgID);;
        String content = "{\n" +
                "  \"dataBody\": {\n" +
                "    \"result\": 1,\n" +
                "    \"reason\": \"\",\n" +
                "    \"rateModelID\": \"" + rateModelID + "\"\n" +
                "  },\n" +
                "  \"header\": {\n" +
                "    \"timeStamp\": \"" + time + "\",\n" +
                "    \"reason\": 1,\n" +
                "    \"function\": \"rateModeSetRes\",\n" +
                "    \"index\": 1,\n" +
                "    \"version\": \"" + versionP + "\"\n" +
                "  }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

    public static void test (){

        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long t = date.getTime();
        String env = "UAT";
        String loginTopic = "200020001/47646059/reLogin";
        String content = "{\n" +
                "    \"dataType\": \"05\",\n" +
                "\"reportDate\":" + t + ",\n" +
                "    \"originDataStr\": \"D2 CF 00 2E 03 02 70 AA 11 81 1B 01 03 01 01 00 00 00 00 00 00 02 70 AA 11 00 00 02 70 AA 11 00 00 00 00 00 00 00 00 00 00 00 00 00 02 70 AA 11 C4 EA\",\n" +
                "    \"tboxCode\": \"47646059\",\n" +
                "    \"xmCarLoginData\": {\n" +
                "        \"allDataStr\": \"D2 CF 00 2E 03 02 70 AA 11 81 1B 01 03 01 01 00 00 00 00 00 00 02 70 AA 11 00 00 02 70 AA 11 00 00 00 00 00 00 00 00 00 00 00 00 00 02 70 AA 11 C4 EA\",\n" +
                "        \"bluetoothMac\": \"00:00:02:70:AA:11\",\n" +
                "        \"hardwareVersion\": \"09.01\",\n" +
                "        \"iccid\": \"47646059\",\n" +
                "        \"loginRespond\": \"SUCCESS\",\n" +
                "        \"simNo\": \"0000000000000270AA11\",\n" +
                "        \"softwareVersion\": \"09.02\",\n" +
                "        \"stateMsg\": \"\",\n" +
                "        \"vinNo\": \"YH019999999999928\",\n" +
                "        \"packageDate\": "+ t +"\n" +
                "    }\n" +
                "}";
        sendMQTT(env, loginTopic, content);
        String loginTopic1 = "100020001/YH019999999999928/reLogin";
        String content1 = "{\n" +
                "    \"dataType\": \"05\",\n" +
                "\"reportDate\":" + t + ",\n" +
                "    \"originDataStr\": \"D2 CF 00 2E 03 02 70 AA 11 81 1B 01 03 01 01 00 00 00 00 00 00 02 70 AA 11 00 00 02 70 AA 11 00 00 00 00 00 00 00 00 00 00 00 00 00 02 70 AA 11 C4 EA\",\n" +
                "    \"tboxCode\": \"47646059\",\n" +
                "    \"xmCarLoginData\": {\n" +
                "        \"allDataStr\": \"D2 CF 00 2E 03 02 70 AA 11 81 1B 01 03 01 01 00 00 00 00 00 00 02 70 AA 11 00 00 02 70 AA 11 00 00 00 00 00 00 00 00 00 00 00 00 00 02 70 AA 11 C4 EA\",\n" +
                "        \"bluetoothMac\": \"00:00:02:70:AA:11\",\n" +
                "        \"hardwareVersion\": \"09.01\",\n" +
                "        \"iccid\": \"47646059\",\n" +
                "        \"loginRespond\": \"SUCCESS\",\n" +
                "        \"simNo\": \"0000000000000270AA11\",\n" +
                "        \"softwareVersion\": \"09.02\",\n" +
                "        \"stateMsg\": \"\",\n" +
                "        \"vinNo\": \"YH019999999999928\",\n" +
                "        \"packageDate\": " + t + "\n" +
                "    }\n" +
                "}";
        sendMQTT(env, loginTopic1, content1);
        String heart = "100020001/YH019999999999928/heartBeat";
        String content2 = "{\n" +
                "  \"dataType\": \"04\",\n" +
                "  \"originDataStr\": \"D2 CF 00 85 03 03 11 37 83 29 18 17 04 0D 0D 2C 03 FF FF FF FF FF FF 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FFFF FF 00 EC FF FF FF FF FF FF FF 00 00 00 07 30 EF 88 01 DF 68 0C FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 03 FF FF FF 10 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 67 FF FF FF FF FF FF FF FF FF FF FF FF 27 7D\",\n" +
                "  \"tboxCode\": \"47646059\",\n" +
                "  \"reportDate\": "+ t +",\n" +
                "  \"xmHeartBeatData\": {\n" +
                "    \"alarmData\": {\n" +
                "      \"alertStateByte\": -1,\n" +
                "      \"alertStateStr\": \"不支持\",\n" +
                "      \"faultStateByte\": -1,\n" +
                "      \"faultStateStr\": \"不支持\"\n" +
                "    },\n" +
                "    \"allDataStr\": \"D2 CF 00 85 03 03 11 37 83 29 18 17 04 0D 0D 2C 03 FF FF FF FF FF FF 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FFFF FF FF FF FF FF FF FF 00 EC FF FF FF FF FF FF FF 00 00 00 07 30 EF 88 01 DF 68 0C FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 03 FF FF FF 10 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 67 FF FF FF FF FF FF FF FF FF FF FF FF 27 7D\",\n" +
                "    \"batteryCode\": \"121511340054\",\n" +
                "    \"batteryCodeByte\": \"////////////////\",\n" +
                "    \"batteryCodeLength\": \"12\",\n" +
                "    \"batteryData\": {\n" +
                "      \"batteryVoltage\": 23.6,\n" +
                "      \"batteryVoltageByte\": \"AOw=\",\n" +
                "      \"remainQuantity\": 91,\n" +
                "      \"remainQuantityByte\": -1,\n" +
                "      \"totalCurrent\": -9999.0,\n" +
                "      \"totalCurrentByte\": \"//8=\",\n" +
                "      \"totalVoltage\": -9999.0,\n" +
                "      \"totalVoltageByte\": \"//8=\"\n" +
                "    },\n" +
                "    \"batteryFactoryCode\": \"7\",\n" +
                "    \"batteryFactoryName\": \"BYD\",\n" +
                "    \"cumulativeChangeCharge\": -9999.0,\n" +
                "    \"cumulativeCharge\": -9999.0,\n" +
                "    \"cumulativeDischarge\": -9999.0,\n" +
                "    \"cumulativeGunCharge\": -9999.0,\n" +
                "    \"cumulativeRequiteCharge\": -9999.0,\n" +
                "    \"currentCarStateByte\": -1,\n" +
                "    \"gprsStrength\": -9999,\n" +
                "    \"gprsStrengthByte\": -1,\n" +
                "    \"iccid\": \"47646059\",\n" +
                "    \"lastCarStateByte\": 0,\n" +
                "    \"locationData\": {\n" +
                "      \"gpsAntennaStateByte\": 0,\n" +
                "      \"gpsAntennaStateStr\": \"正常\",\n" +
                "      \"gpsCN\": 0,\n" +
                "      \"gpsCNByte\": 0,\n" +
                "      \"gpsStateByte\": 0,\n" +
                "      \"gpsStateStr\": \"有效定位\",\n" +
                "      \"lat\": \"31.369088\",\n" +
                "      \"latByte\": \"Ad9oDA==\",\n" +
                "      \"lng\": \"120.642663\",\n" +
                "      \"lngByte\": \"BzDviA==\"\n" +
                "    },\n" +
                "    \"logger\": {\n" +
                "      \"debugEnabled\": false,\n" +
                "      \"effectiveLevel\": {\n" +
                "        \"levelInt\": 20000,\n" +
                "        \"levelStr\": \"INFO\"\n" +
                "      },\n" +
                "      \"errorEnabled\": true,\n" +
                "      \"infoEnabled\": true,\n" +
                "      \"name\": \"cn.wxzd.collector.parser.tbox.cmd.xm.heartbeat.data.XMHeartBeatData\",\n" +
                "      \"traceEnabled\": false,\n" +
                "      \"warnEnabled\": true\n" +
                "    },\n" +
                "    \"number\": \"FFFFFFFFFFFFFFFFFFFFFFFF\",\n" +
                "    \"packageDate\": "+ t +",\n" +
                "    \"remainMileage\": -9999.0,\n" +
                "    \"remainMileageByte\": \"//8=\",\n" +
                "    \"reserve\": \"FFFFFFFF\",\n" +
                "    \"singleCharge\": 0.0,\n" +
                "    \"sohByte\": -1,\n" +
                "    \"sohStr\": -9999.0,\n" +
                "    \"speed\": \"10\",\n" +
                "    \"speedByte\": -1,\n" +
                "    \"stateMsg\": \"\",\n" +
                "    \"temperature\": -9999,\n" +
                "    \"temperatureByte\": -1,\n" +
                "    \"totalMileage\": -9999.0,\n" +
                "    \"totalMileageByte\": \"////\",\n" +
                "    \"upRespond\": \"SUCCESS\",\n" +
                "    \"vehicleData\": {\n" +
                "      \"accStateByte\": 1,\n" +
                "      \"accStateStr\": \"ACC\",\n" +
                "      \"airConditionerStateByte\": -1,\n" +
                "      \"airConditionerStateStr\": \"不支持\",\n" +
                "      \"allOilBurn\": -9999.0,\n" +
                "      \"altitude\": -9999,\n" +
                "      \"batteryStateByte\": -1,\n" +
                "      \"batteryStateStr\": \"不支持\",\n" +
                "      \"currentLoad\": -9999,\n" +
                "      \"doorStateByte\": -1,\n" +
                "      \"doorStateStr\": \"11111111\",\n" +
                "      \"energyType\": 3,\n" +
                "      \"energyTypeName\": \"福田\",\n" +
                "      \"gearByte\": -1,\n" +
                "      \"gearStr\": \"不支持\",\n" +
                "      \"handbrakeStateByte\": -1,\n" +
                "      \"handbrakeStateStr\": \"不支持\",\n" +
                "      \"heartAirConditionerStateStr\": \"不支持\",\n" +
                "      \"horizontal\": 65535,\n" +
                "      \"keyStateByte\": 0,\n" +
                "      \"keyStateStr\": \"不支持\",\n" +
                "      \"lightsStateByte\": -1,\n" +
                "      \"lightsStateStr\": \"11111111\",\n" +
                "      \"lockStateByte\": 0,\n" +
                "      \"lockStateStr\": \"不支持\",\n" +
                "      \"pitchAngle\": -74,\n" +
                "      \"revolationSpeed\": 65535,\n" +
                "      \"stateMsg\": \"\",\n" +
                "      \"surplusOil\": -9999,\n" +
                "      \"tempe\": 65535,\n" +
                "      \"trunkStateByte\": -1,\n" +
                "      \"trunkStateStr\": \"不支持\",\n" +
                "      \"windowStateByte\": -1,\n" +
                "      \"windowStateStr\": \"11111111\",\n" +
                "      \"xAcceleration\": 65535,\n" +
                "      \"xPalstance\": 65535,\n" +
                "      \"yAcceleration\": 65535,\n" +
                "      \"yPalstance\": 65535,\n" +
                "      \"zAcceleration\": 65535,\n" +
                "      \"zPalstance\": 65535\n" +
                "    },\n" +
                "    \"vinNo\": \"YH019999999999928\",\n" +
                "    \"vipCardNo\": \"-1\"\n" +
                "  }\n" +
                "}";
        sendMQTT(env, heart, content2);
        String heart1 = "200020001/47646059/heartBeat";
        String content3 = " {\n" +
                "  \"dataType\": \"04\",\n" +
                "  \"originDataStr\": \"D2 CF 00 85 03 03 11 37 83 29 18 17 04 0D 0D 2C 03 FF FF FF FF FF FF 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FFFF FF 00 EC FF FF FF FF FF FF FF 00 00 00 07 30 EF 88 01 DF 68 0C FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 03 FF FF FF 10 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 67 FF FF FF FF FF FF FF FF FF FF FF FF 27 7D\",\n" +
                "  \"tboxCode\": \"47646059\",\n" +
                "  \"reportDate\": "+ t +",\n" +
                "  \"xmHeartBeatData\": {\n" +
                "    \"alarmData\": {\n" +
                "      \"alertStateByte\": -1,\n" +
                "      \"alertStateStr\": \"不支持\",\n" +
                "      \"faultStateByte\": -1,\n" +
                "      \"faultStateStr\": \"不支持\"\n" +
                "    },\n" +
                "    \"allDataStr\": \"D2 CF 00 85 03 03 11 37 83 29 18 17 04 0D 0D 2C 03 FF FF FF FF FF FF 01 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FFFF FF FF FF FF FF FF FF 00 EC FF FF FF FF FF FF FF 00 00 00 07 30 EF 88 01 DF 68 0C FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 03 FF FF FF 10 FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF 67 FF FF FF FF FF FF FF FF FF FF FF FF 27 7D\",\n" +
                "    \"batteryCode\": \"121511340054\",\n" +
                "    \"batteryCodeByte\": \"////////////////\",\n" +
                "    \"batteryCodeLength\": \"12\",\n" +
                "    \"batteryData\": {\n" +
                "      \"batteryVoltage\": 23.6,\n" +
                "      \"batteryVoltageByte\": \"AOw=\",\n" +
                "      \"remainQuantity\": 91,\n" +
                "      \"remainQuantityByte\": -1,\n" +
                "      \"totalCurrent\": -9999.0,\n" +
                "      \"totalCurrentByte\": \"//8=\",\n" +
                "      \"totalVoltage\": -9999.0,\n" +
                "      \"totalVoltageByte\": \"//8=\"\n" +
                "    },\n" +
                "    \"batteryFactoryCode\": \"7\",\n" +
                "    \"batteryFactoryName\": \"BYD\",\n" +
                "    \"cumulativeChangeCharge\": -9999.0,\n" +
                "    \"cumulativeCharge\": -9999.0,\n" +
                "    \"cumulativeDischarge\": -9999.0,\n" +
                "    \"cumulativeGunCharge\": -9999.0,\n" +
                "    \"cumulativeRequiteCharge\": -9999.0,\n" +
                "    \"currentCarStateByte\": -1,\n" +
                "    \"gprsStrength\": -9999,\n" +
                "    \"gprsStrengthByte\": -1,\n" +
                "    \"iccid\": \"47646059\",\n" +
                "    \"lastCarStateByte\": 0,\n" +
                "    \"locationData\": {\n" +
                "      \"gpsAntennaStateByte\": 0,\n" +
                "      \"gpsAntennaStateStr\": \"正常\",\n" +
                "      \"gpsCN\": 0,\n" +
                "      \"gpsCNByte\": 0,\n" +
                "      \"gpsStateByte\": 0,\n" +
                "      \"gpsStateStr\": \"有效定位\",\n" +
                "      \"lat\": \"31.369088\",\n" +
                "      \"latByte\": \"Ad9oDA==\",\n" +
                "      \"lng\": \"120.642663\",\n" +
                "      \"lngByte\": \"BzDviA==\"\n" +
                "    },\n" +
                "    \"logger\": {\n" +
                "      \"debugEnabled\": false,\n" +
                "      \"effectiveLevel\": {\n" +
                "        \"levelInt\": 20000,\n" +
                "        \"levelStr\": \"INFO\"\n" +
                "      },\n" +
                "      \"errorEnabled\": true,\n" +
                "      \"infoEnabled\": true,\n" +
                "      \"name\": \"cn.wxzd.collector.parser.tbox.cmd.xm.heartbeat.data.XMHeartBeatData\",\n" +
                "      \"traceEnabled\": false,\n" +
                "      \"warnEnabled\": true\n" +
                "    },\n" +
                "    \"number\": \"FFFFFFFFFFFFFFFFFFFFFFFF\",\n" +
                "    \"packageDate\": "+ t +",\n" +
                "    \"remainMileage\": -9999.0,\n" +
                "    \"remainMileageByte\": \"//8=\",\n" +
                "    \"reserve\": \"FFFFFFFF\",\n" +
                "    \"singleCharge\": 0.0,\n" +
                "    \"sohByte\": -1,\n" +
                "    \"sohStr\": -9999.0,\n" +
                "    \"speed\": \"10\",\n" +
                "    \"speedByte\": -1,\n" +
                "    \"stateMsg\": \"\",\n" +
                "    \"temperature\": -9999,\n" +
                "    \"temperatureByte\": -1,\n" +
                "    \"totalMileage\": -9999.0,\n" +
                "    \"totalMileageByte\": \"////\",\n" +
                "    \"upRespond\": \"SUCCESS\",\n" +
                "    \"vehicleData\": {\n" +
                "      \"accStateByte\": 1,\n" +
                "      \"accStateStr\": \"ACC\",\n" +
                "      \"airConditionerStateByte\": -1,\n" +
                "      \"airConditionerStateStr\": \"不支持\",\n" +
                "      \"allOilBurn\": -9999.0,\n" +
                "      \"altitude\": -9999,\n" +
                "      \"batteryStateByte\": -1,\n" +
                "      \"batteryStateStr\": \"不支持\",\n" +
                "      \"currentLoad\": -9999,\n" +
                "      \"doorStateByte\": -1,\n" +
                "      \"doorStateStr\": \"11111111\",\n" +
                "      \"energyType\": 3,\n" +
                "      \"energyTypeName\": \"福田\",\n" +
                "      \"gearByte\": -1,\n" +
                "      \"gearStr\": \"不支持\",\n" +
                "      \"handbrakeStateByte\": -1,\n" +
                "      \"handbrakeStateStr\": \"不支持\",\n" +
                "      \"heartAirConditionerStateStr\": \"不支持\",\n" +
                "      \"horizontal\": 65535,\n" +
                "      \"keyStateByte\": 0,\n" +
                "      \"keyStateStr\": \"不支持\",\n" +
                "      \"lightsStateByte\": -1,\n" +
                "      \"lightsStateStr\": \"11111111\",\n" +
                "      \"lockStateByte\": 0,\n" +
                "      \"lockStateStr\": \"不支持\",\n" +
                "      \"pitchAngle\": -74,\n" +
                "      \"revolationSpeed\": 65535,\n" +
                "      \"stateMsg\": \"\",\n" +
                "      \"surplusOil\": -9999,\n" +
                "      \"tempe\": 65535,\n" +
                "      \"trunkStateByte\": -1,\n" +
                "      \"trunkStateStr\": \"不支持\",\n" +
                "      \"windowStateByte\": -1,\n" +
                "      \"windowStateStr\": \"11111111\",\n" +
                "      \"xAcceleration\": 65535,\n" +
                "      \"xPalstance\": 65535,\n" +
                "      \"yAcceleration\": 65535,\n" +
                "      \"yPalstance\": 65535,\n" +
                "      \"zAcceleration\": 65535,\n" +
                "      \"zPalstance\": 65535\n" +
                "    },\n" +
                "    \"vinNo\": \"YH019999999999928\",\n" +
                "    \"vipCardNo\": \"-1\"\n" +
                "  }\n" +
                "}";
        sendMQTT(env, heart1, content3);


    }

    public static Boolean sendPowerAdjustSetReq(String env, String productcode, String stationid, String chgID, String snno, String result) {
        String pubTopic = productcode + "/" + stationid + "/S2M/response";
        String version = Query.getVersion(env, stationid);
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String content = "{\n" +
                "  \"header\": {\n" +
                "    \"version\": \"" + version + "\",\n" +
                "    \"timeStamp\": \"" + dateFormat.format(date) + "\",\n" +
                "    \"index\": " + snno + ",\n" +
                "    \"function\": \"powerAdjustSetResp\"\n" +
                "  },\n" +
                "  \"dataBody\": {\n" +
                "    \"chgID\": " + chgID + ",\n" +
                "    \"gunNo\": 0,\n" +
                "    \"result\": " + result + "\n" +
                "  }\n" +
                "}";

        return sendMQTT(env, pubTopic, content);
    }

}


